﻿using Douyu.Api.Entity;
using Douyu.Common;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Douyu.Api
{
    /// <summary>
    /// 斗鱼API客户端
    /// </summary>
    public class DouyuApiClient
    {
        /// <summary>
        /// 单例化
        /// </summary>
        private DouyuApiClient()
        {
        }

        /// <summary>
        /// 单例句柄
        /// </summary>
        public static DouyuApiClient Instance { get; set; } = new DouyuApiClient();

        /// <summary>
        /// 接收登陆后Token
        /// </summary>
        public string Token { get; set; } = string.Empty;

        #region 获取房间列表

        /// <summary>
        /// 获取房间列表
        /// </summary>
        /// <param name="cateId"></param>
        /// <param name="cateName"></param>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<IEntity<IEnumerable<RoomsEntity>>> GetRoomsAsync(
            int? cateId = null,
            string cateName = null,
            int offset = 0,
            int limit = 10
        )
        {
            return await DouyuApiUrl.Rooms
                .Fmt(cateId.ToRoomKey(cateName), offset, limit)
                .ToEntityListAsync<RoomsEntity>();
        }

        #endregion

        #region 获取房间详情

        /// <summary>
        /// 获取房间详情
        /// </summary>
        /// <param name="cateId"></param>
        /// <param name="cateName"></param>
        /// <returns></returns>
        public async Task<IEntity<RoomEntity>> GetRoomAsync(int? cateId = null, string cateName = null)
        {
            return await DouyuApiUrl.Room
                .Fmt(cateId.ToRoomKey(cateName))
                .ToEntityAsync<RoomEntity>();
        }

        #endregion

        #region 获取游戏分类

        /// <summary>
        /// 获取游戏分类
        /// </summary>
        /// <returns></returns>
        public async Task<IEntity<IEnumerable<GameCategoryEntity>>> GetGameCategoryAsync()
        {
            return await DouyuApiUrl.GameCategory
                .ToEntityListAsync<GameCategoryEntity>();
        }

        #endregion

        #region 搜索房间/主播

        /// <summary>
        /// 搜索房间/主播
        /// </summary>
        /// <param name="key"></param>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<IEntity<IEnumerable<SearchEntity>>> SearchAsync(
            string key,
            int offset = 0,
            int limit = 10
        )
        {
            return await DouyuApiUrl.Search
                .Fmt(key, offset, limit)
                .ToEntityListAsync<SearchEntity>();
        }

        #endregion

        #region 登陆

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<IEntity<UserEntity>> LoginAsync(string userName, string pwd)
        {
            return await DouyuApiUrl.Login
                .Fmt(userName, pwd.ToMD5())
                .ToEntityAsync<UserEntity>();
        }

        #endregion

        #region 用户详情

        /// <summary>
        /// 用户详情
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<IEntity<UserEntity>> GetUserAsync(string token)
        {
            return await DouyuApiUrl.User
                .Fmt(token)
                .ToEntityAsync<UserEntity>();
        }

        #endregion

        #region 关注列表

        /// <summary>
        /// 关注列表
        /// </summary>
        /// <param name="token"></param>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<IEntity<IEnumerable<FollowEntity>>> GetFollowAsync(
            string token,
            int offset = 0,
            int limit = 10
        )
        {
            return await DouyuApiUrl.Follow
                .Fmt(token)
                .ToEntityListAsync<FollowEntity>();
        }

        #endregion

        #region 历史列表

        /// <summary>
        /// 历史列表
        /// </summary>
        /// <param name="token"></param>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<IEntity<IEnumerable<HistoryEntity>>> GetHistoryAsync(
            string token,
            int offset = 0,
            int limit = 10
        )
        {
            return await DouyuApiUrl.History
                .Fmt(token)
                .ToEntityListAsync<HistoryEntity>();
        }

        #endregion

    }

}
